import { GET_TOKEN_URL, HOME_PATH } from "@/enums/routerEnum";
import { setCache } from "@/utils/cache";

/*
 * @Description:
 * @Date: 2023-01-09 14:47:07
 * @LastEditors: czp
 * @LastEditTime: 2023-02-22 16:21:31
 */
export const useVR = () => {
	const $router = useRouter();
	const $route = useRoute();
	/**
	 * 页面跳转
	 * @param {String} path path
	 * @param {Object} query query
	 */
	const pageJump = (path: string, query: Record<string, any> = {}, type = "push" as "push" | "replace") => {
		$router[type]({ path, query });
	};

	/**
	 * 返回上一页
	 */
	const back = () => {
		$router.back();
	};

	/**
	 * 前往首页
	 */
	const toHome = () => {
		pageJump(HOME_PATH, null, "replace");
	};

	const login = () => {
		setCache("toPath", $route?.path);
		setCache("toQuery", $route?.query);
		window.location.assign(GET_TOKEN_URL);
	};

	return { back, pageJump, toHome, login };
};
